//===============================================================================
//	BaseMessage
//===============================================================================

#pragma once

#include <string>
#include "../../3rd/libjson/json.h"
#include "../share/user_define.h"

#include "../share/MessageDef.h"

class Player;

class BaseMessage
{
public:
	virtual ~BaseMessage(){}

public:
	virtual void HandleMessage(Player* pplayer) = 0;

public:
	bool ParseAcceptJson(std::string* accept)
	
	{
		_safe_guard_begin;

		if (accept->empty())
		{
			return false;
		}

		if (! JsonReader(*accept, m_jv))
		{
			return false;
		}

		if(m_jv.isMember("uid"))
		{
			m_client_sockid = m_jv["uid"].asUInt();	
		}

		if (m_jv.isMember("msgReturn"))
		{
			m_from_sockid = m_jv["msgReturn"]["sid"].asUInt();
		}

		return true;

		_safe_guard_end;

		return false;
	}

	//---------------------------------------------------------------------------
	virtual void Clear(void)
	{
		_safe_guard_begin;

		m_send.clear();
		m_jv.clear();

		_safe_guard_end;
	}
	
public:
	//---------------------------------------------------------------------------
	Json::Value* GetJsonValue(void)
	{
		_safe_guard_begin;

		return &m_jv;

		_safe_guard_end;

		return nullptr;
	}

	//---------------------------------------------------------------------------
	std::string* GetSendString(void)
	{
		_safe_guard_begin;

		createSendToClientString();
		return &m_send;

		_safe_guard_end;

		return nullptr;
	}

	//---------------------------------------------------------------------------
	std::string* GetSendToServerString(void)
	{
		_safe_guard_begin;

		createSendToServerString();
		return &m_send;

		_safe_guard_end;

		return nullptr;
	}

	//---------------------------------------------------------------------------
	void SetSendToServerId(uint32_t sendType, uint32_t clientId)
	{
		_safe_guard_begin;

		m_client_sockid = clientId;
		m_jv["tt"] = sendType;

		_safe_guard_end;
	}

	//---------------------------------------------------------------------------
	void ResetFromSockId(uint32_t uid)
	{
		_safe_guard_begin;

		m_from_sockid = uid;

		_safe_guard_end;
	}

	uint32_t GetProtocolNumber(void)
	{
		_safe_guard_begin;

		return m_pn;

		_safe_guard_end;
	}

	void ResetProtocolNumber(uint32_t pn)
	{
		_safe_guard_begin;

		m_pn = pn;

		_safe_guard_end;
	}

	uint32_t GetFromSocketId(void)
	{
		_safe_guard_begin;

		return m_from_sockid;

		_safe_guard_end;
	}

private:
	//---------------------------------------------------------------------------
	void createSendToClientString(void)
	{
		_safe_guard_begin;

		m_jv["msgid"] = m_pn;
		m_jv["uid"] = m_from_sockid;
		m_send = JsonWriter(m_jv);

		_safe_guard_end;
	}

	//---------------------------------------------------------------------------
	void createSendToServerString(void)
	{
		_safe_guard_begin;

		m_jv["msgid"] = m_pn;
		m_jv["uid"] = m_client_sockid;
		m_send = JsonWriter(m_jv);

		_safe_guard_end;
	}


protected:
	//---------------------------------------------------------------------------
	BaseMessage(uint32_t uid, uint32_t pn)
		: m_from_sockid(uid)
		, m_client_sockid(0)
		, m_pn(pn)
	{
		_safe_guard_begin;

		_safe_check(0 < m_pn && m_pn < max_message_id);

		_safe_guard_end;
	}

protected:
	std::string	m_send;			//	send message string
	Json::Value m_jv;			//	json value
	uint32_t	m_from_sockid;	//	socket id
	uint32_t 	m_client_sockid;//  have "tt"
	uint32_t	m_pn;			//	protocol number
};